package com.htl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.htl.entity.User;
import com.htl.exception.Assert;
import com.htl.exception.ShopException;
import com.htl.form.UserLoginForm;
import com.htl.form.UserRegisterForm;
import com.htl.result.ResponseEnum;
import com.htl.service.UserService;
import com.htl.util.JwtUtil;
import com.htl.util.MD5Util;
import com.htl.util.RegexValidateUtil;
import com.htl.util.ResultVOUtil;
import com.htl.vo.ResultVO;
import com.htl.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 韩天乐
 * @since 2021-11-14
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/register")
    public ResultVO register(@Valid @RequestBody UserRegisterForm userRegisterForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            throw new ShopException(ResponseEnum.USER_INFO_NULL.getMsg());
        }
        // 判断手机号是否正确
        Assert.isTrue(RegexValidateUtil.checkMobile(userRegisterForm.getMobile()), ResponseEnum.MOBILE_ERROR);
        // 判断手机号是否已经注册
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", userRegisterForm.getMobile());
        User one = this.userService.getOne(queryWrapper);
        if (one != null) {
            throw new ShopException(ResponseEnum.USER_MOBILE_EXIST.getMsg());
        }
//        String code = (String) this.redisTemplate.opsForValue().get("uushop-sms-code-" + userRegisterForm.getMobile());
//        Assert.equals(code, userRegisterForm.getCode(), ResponseEnum.USER_CODE_ERROR);
        // 注册
        User user = new User();
        user.setMobile(userRegisterForm.getMobile());
        user.setPassword(MD5Util.getSaltMD5(userRegisterForm.getPassword()));
        boolean save = this.userService.save(user);
        if(save) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(null);
    }


    @GetMapping("/login")
    public ResultVO login(@Valid UserLoginForm userLoginForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new ShopException(ResponseEnum.USER_INFO_NULL.getMsg());
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", userLoginForm.getMobile());
        User one = this.userService.getOne(queryWrapper);
        if (one == null) {
            throw new ShopException(ResponseEnum.USER_MOBILE_NULL.getMsg());
        }
        boolean saltverifyMD5 = MD5Util.getSaltverifyMD5(userLoginForm.getPassword(), one.getPassword());
        if (!saltverifyMD5) {
            throw new ShopException(ResponseEnum.PASSWORD_ERROR.getMsg());
        }
        UserVO userVO = new UserVO();
        userVO.setMobile(one.getMobile());
        userVO.setUserId(one.getUserId());
        userVO.setPassword(one.getPassword());
        userVO.setToken(JwtUtil.createToken(one.getUserId(), one.getMobile()));
        return ResultVOUtil.success(userVO);
    }


    @GetMapping("/checkToken")
    public ResultVO checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        boolean b = JwtUtil.checkToken(token);
        if(b) return ResultVOUtil.success(null);
        return ResultVOUtil.fail(null);
    }

}

